Return to doc.sitecore.com

1.  Structs Changed to Classes
Prev Next

One of the major API changes and also one of the greatest reasons of the speed increase is that ID, Language and Version have changed from Structs to classes. Our development team found that these Structs were accessed by XSLT renderings thousands of times per each XSLT statement which added boxing overhead and ended up hurting performance.   

Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well. Structs can declare constructors, and a default constructor is always provided to initialize the struct members to their default values. 

 

Which effect will this have on existing sites?

If the solution has been developed using no .NET code-behind (e.g. using only XSLT), it will have no effect, though you will naturally need to upgrade your Sitecore installation and database. 

If the solution is using .NET code-behind then problems may occur if the solution has not been developed in correspondence with good code-ethics: For example, if you start using a variable that was previously declared on a struct and start assigning values to it without initializing the variable (it was a struct, and the struct has a default constructor), it will now result in an “Object not initialized” error.


For example, the following did not fail in Sitecore 5.0.x while it will fail in Sitecore 5.1:

 

class myApplication {

 

  Sitecore.Data.ID myID;

 

  Void myMethod {

   System.Guid myGuid = myID.ToGuid();

  }

}

 

The correct code is:

 

class myApplication {

 

  Sitecore.Data.ID myID;

 

  Void myMethod {

    myID =  new Sitecore.Data.ID();

    System.Guid myGuid = myID.ToGuid();

  }

}

 


Prev Next